<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Validate;
use think\model\concern\SoftDelete;

class StudentTypeParams extends Common
{
    protected $rule = [
        'type_id'       => 'require',
        'name'          => 'require|max:20',
        'sort'          => 'number',
    ];
    protected $msg = [
        'type_id.require'  => '请选择类型',
        'name.require'  => '请输入参数名称',
        'name.max'      => '参数名称长度最大20位',
        'sort'          => '排序必须为数字'
    ];

    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->alias('p')
                ->join('zl_student_type t', 't.id = p.type_id')
                ->field('p.*, t.name as type_name')
                ->where($tableWhere['where'])
                ->order($tableWhere['order'])
                ->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    /**
     *  添加品牌
     * User:tony
     * @param $data
     * @return array
     */
    public function addData($data)
    {
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data'=> []
        ];
        $validate = new Validate($this->rule,$this->msg);
        if (!$validate->check($data)) {
            $result['status'] = false;
            $result['msg'] = $validate->getError();
        } else {
            // 判断是否存在相同的参数
            $info = $this->where(['name'=>$data['name'], 'type_id'=>$data['type_id']])->find();
            if($info){
                $result['status'] = false;
                $result['msg'] = '存在名称相同的参数';   
            } else {
                if (!$this->allowField(true)->save($data))
                {
                    $result['status'] = false;
                    $result['msg'] = '保存失败';
                }
            }
        }
        \think\facade\Request::token('__ZlServer_Token__', 'sha1');
        return $result;
    }


    /**
     *  修改品牌
     * User:tony
     * @param $data
     * @return array
     */
    public function saveData($data)
    {
        $result = [
            'status' => true,
            'msg' => '保存成功',
            'data' => []
        ];
        $validate = new Validate($this->rule,$this->msg);
        if (!$validate->check($data)) {
            $result['status'] = false;
            $result['msg'] = $validate->getError();
        } else {
            // 判断是否存在相同的参数
            $info = $this->where(['name'=>$data['name'], 'type_id'=>$data['type_id']])->where('id','neq',$data['id'])->find();
            if($info){
                $result['status'] = false;
                $result['msg'] = '存在名称相同的参数';   
            } else {
                if (!$this->allowField(true)->save($data,['id'=>$data['id']]))
                {
                    $result['status'] = false;
                    $result['msg'] = '保存失败';
                }
            }
        }
        \think\facade\Request::token('__ZlServer_Token__', 'sha1');
        return $result;
    }


    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['name']) && $post['name'] != ""){
            $where[] = ['p.name', 'like', '%'.$post['name'].'%'];
        } 
        if(isset($post['type_id']) && $post['type_id'] != ""){
            $where[] = ['type_id', 'eq', $post['type_id']];
        } 
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['type_id', 'sort ASC'];
        return $result;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    public function getNameList()
    {
        $list = $this->field(['name'])->select()->toArray();
        
        $nameList = [];
        foreach ($list as $key => $val) {
            $nameList[] = $val['name'];
        }
        
        return $nameList;
    }
}